{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "iam a test\n"
     ]
    }
   ],
   "source": [
    "def test():\n",
    "    print(\"iam a test\")\n",
    "if __name__==\"__main__\":\n",
    "    test()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "from pathlib import Path\n",
    "import os\n",
    "import keras\n",
    "from keras.preprocessing.image import ImageDataGenerator\n",
    "import matplotlib.pyplot as plt \n",
    "import cv2 as cv\n",
    "import random\n",
    "\n",
    "\n",
    "\n",
    "#coding:utf-8\n",
    "from keras.preprocessing.image import ImageDataGenerator,load_img,img_to_array\n",
    "from PIL import Image,ImageEnhance\n",
    "#定义图片生成器\n",
    "\n",
    " \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "# load and to numpy\n",
    "def load_and_npy(file_path):\n",
    "    img=load_img(file_path)\n",
    "    x = img_to_array(img,data_format=\"channels_last\")    \n",
    "    x=x.reshape((1,) + x.shape)\n",
    "    return x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "#改变亮度\n",
    "def brigth(img,save_path,param=1):\n",
    "    #print(save_path)\n",
    "    dst_path=Path(save_path).joinpath(\"brigth.jpg\")\n",
    "    print(dst_path)\n",
    "    enhancer = ImageEnhance.Brightness(img)  \n",
    "    with open(dst_path,\"wb\") as f:\n",
    "        enhancer.enhance(param).save(f)\n",
    "\n",
    "    return load_and_npy(dst_path)\n",
    "\n",
    "#改变颜色\n",
    "def color(img,save_path,param=1):\n",
    "    dst_path=Path(save_path).joinpath(\"color.jpg\")\n",
    "    enhancer = ImageEnhance.Color(img)  \n",
    "    with open(dst_path,\"wb\") as f:\n",
    "        enhancer.enhance(param).save(f)\n",
    "    \n",
    "    return load_and_npy(dst_path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "X:\\share\\image\\lfw_classfiy_image_equel1_gener\\Abdullah_Gul\\Abdullah_Gul_0001.png\n",
      "X:\\share\\image\\lfw_classfiy_image_equel1_gener\\Abdullah_Gul\\brigth.jpg\n",
      "X:\\share\\image\\lfw_classfiy_image_equel1_gener\\Abdullah_Gul\\brigth.jpg\n",
      "X:\\share\\image\\lfw_classfiy_image_equel1_gener\\Abdullah_Gul\\brigth.jpg\n"
     ]
    }
   ],
   "source": [
    "import random\n",
    "\n",
    "\n",
    "\n",
    "def gener_image(src_path,dst_path):\n",
    "    imgfile=Path(src_path).rglob(\"*.png\").__next__()\n",
    "    print(imgfile)\n",
    "    img=load_img(imgfile)\n",
    "    \n",
    "    x = img_to_array(img,data_format=\"channels_last\")   #图片转化成array类型,因flow()接收numpy数组为参数\n",
    "    x=x.reshape((1,) + x.shape)     #要求为4维\n",
    "    # 旋转与翻转\n",
    "    data_gen = ImageDataGenerator(rotation_range=5,\n",
    "                                  width_shift_range=0,\n",
    "                                  height_shift_range=0,\n",
    "                                  horizontal_flip=True,\n",
    "                                  vertical_flip=False,cval=0,\n",
    "                                  fill_mode='constant',\n",
    "                                  )\n",
    "\n",
    "    for i in range(3):\n",
    "        plist=( img, dst_path,random.choice([0.5,0.6,0.8,1.2,1.4,1.5]) )\n",
    "        pmap={\"save_to_dir\":dst_path,\"save_prefix\":'flower',\"save_format\":'jpg'}\n",
    "        data_gen.flow(x,**pmap).__next__()\n",
    "\n",
    "        data_gen.flow(color(*plist), **pmap).__next__()\n",
    "\n",
    "        data_gen.flow(brigth(*plist), **pmap).__next__()\n",
    "\n",
    "# if __name__==\"__main__\":\n",
    "#     src_path=r\"X:\\share\\image\\lfw_classfiy_image_equel1_gener\\Abdullah_Gul\"\n",
    "#     dst_path=r\"X:\\share\\image\\lfw_classfiy_image_equel1_gener\\Abdullah_Gul\"\n",
    "#     gener_image(src_path,dst_path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.6"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
